//
//  NoteMapView.m
//  geoNotes
//
//  Created by Administrator on 6/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NoteMapView.h"
#import "NoteAnnotation.h"

@implementation NoteMapView
@synthesize mapView,delegate,db,notedb;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [mapView release];
    [db release];
    [notedb release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setMapView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"database count: %d",[db count]);
    if([db count] <1)
        return;
    CLLocationCoordinate2D mapCenter;    
    MKCoordinateSpan mapSpan;
    mapSpan.latitudeDelta = 0.005;
    mapSpan.longitudeDelta = 0.005;
    MKCoordinateRegion mapRegion;
    
    
    for (Note *note in [notedb database]) {
        mapCenter.latitude = [note.lat doubleValue];
        mapCenter.longitude = [note.lon doubleValue];
        [mapView addAnnotation:[[[NoteAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake([note.lat doubleValue],[note.lon doubleValue]) andTitle:note.title andSubtitle:[note.message substringToIndex:10] andIndex:[db indexOfObject:note] ]autorelease]];
    }
    mapRegion.center = mapCenter;
    mapRegion.span = mapSpan;
    NSLog(@"user position: %f,%f",mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude);
    self.mapView.region = mapRegion;
    self.mapView.mapType = MKMapTypeStandard;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    NoteAnnotation* temp=[view annotation];
    NoteDetailsView*controller = [[NoteDetailsView alloc] initWithNibName:@"NoteDetailsView" bundle:nil];
    controller.delegate = self;
    controller.note=[db objectAtIndex:temp.mNoteIndex];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];
    

}
-(void) noteDetailsViewDidFinish{
    [self dismissModalViewControllerAnimated:YES];
}
-(void)noteDetailsViewDidFinish:(UIViewController *)controller withDelete:(Note *)note{
   
    for (NoteAnnotation* annotation in mapView.annotations) {
        
            [mapView removeAnnotation:annotation];
        
    }
    
    [notedb deleteNote:note];
    NSLog(@"notes in db %d",[notedb notesInDB]);
    
    [self dismissModalViewControllerAnimated:YES];
}
-(void)updateAnnotions{
   
  
}
- (IBAction)onReturn:(id)sender {
    [self.delegate noteMapViewDidFinish];
}
@end
